package s.k.gui.webtest.element;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

import s.k.gui.webtest.Allocator;
import s.k.gui.webtest.BrowserWindow;
import s.k.gui.webtest.WebDriverBrowserWindow;

public class RichTextField extends Element {

	public RichTextField(String comment) {

		super(comment);

	}


	public void setText(String text) {

		StringBuilder message = new StringBuilder();

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (isExist()) {

			message.append(this.comment + ":");

			if (currentWindow instanceof WebDriverBrowserWindow) {

				String js = "contentWindow.document.body.innerText=\'" + text
						+ "\';";

				WebDriver driver = (WebDriver) currentWindow.getDriver()
						.getEngine();

				((JavascriptExecutor) driver).executeScript(js);
			}
		}
	}

	
	public void clearBodyText() {

		StringBuilder message = new StringBuilder();

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (isExist()) {

			message.append(this.comment + ":");

			if (currentWindow instanceof WebDriverBrowserWindow) {

				String js = "contentWindow.document.body.innerText=\'\';";

				WebDriver driver = (WebDriver) currentWindow.getDriver()
						.getEngine();

				((JavascriptExecutor) driver).executeScript(js);

			}

			message.append("清理成功！");

			logger.info(message);

		} else {

			message.append(this.comment + ":");

			message.append("没有找到元素！");

			logger.error(message);
		}
	}
}
